<?php
/**
 * @Author: xuzhiyi
 * @Date:   2016-02-16 20:57:17
 * @Last Modified by:   xuzhiyi
 * @Last Modified time: 2016-02-19 18:58:24
 * ------------------------------------------------ *
 * 商品分类                                         *
 * ------------------------------------------------ */
namespace Admin\Model;

class GoodsCatsModel extends CommonModel{

	/* 改变状态
	 *--------------------------*/
	public function editStatus(){
	 	$g = M('goods_cats');
	 	$data['catFlag'] = (I('catFlag')==1)?1:0;
	 	$rs = $g->where("catId=".(int)I('catId',0))->save($data);
	    if(false !== $rs){
			return true;
		}
	 	return false;
	}

	/* 修改排序
	 * -------------------*/
	public function editSort(){
		$jsonData = json_encode(I('data'));
		$data=$this->getJsonData($jsonData);
		foreach($data as $key=>$val){
			
			$catid=$val['id'];

			$rs=M('goods_cats')->where('catId='.$catid)->setField('catSort',$key);
			if(false===$rs){
				return false;
			}
		}
		return true;
	}


	public function getCats(){
		$g=M('goods_cats');
		$rs =$g->where('catFlag!=-1')->select();
		return getTree($rs);
	}


	//获取分类
	public function getGoodsCats(){
		$rs =M('goods_cats')
		->join('as a left join '.C("DB_PREFIX").'goods_cats as b on a.parentId=b.catId')
		->field('a.*,b.catName as parentName')
		->where('a.catFlag!=-1')
		->order('a.catSort asc')
		->select();
		return $rs;
	}

	//添加分类
	public function addGoodsCats(){
		$parentId = (int)I("parentId");
		if($parentId == 0){
			$catpath = '0,';
		}else{
			$rs=M('goods_cats')->field("concat(catPath,catId,',') as catPath")
				->where("catId=".$parentId)->find();
			$catpath = $rs['catPath'];
		}
		
		$data = array();
		$data["catName"] = trim(I("catName"));		
		$data["parentId"] = $parentId;
		$data["catPath"] = $catpath;
		$data["catFlag"] = 1;
	    if($this->checkEmpty($data)){
	    	$data["priceSection"] = trim(I("priceSection"));
			$rs = M('goods_cats')->add($data);
			if(false !== $rs){
				return true;
			}
		}
		return false;
	}

	/* 修改分类
	 *--------------------------*/
	 public function editGoodsCats(){
	 	$catId = (int)I("catId");
	 	$parentId = (int)I("parentId");
		if($parentId == 0){
			$catpath = '0,';
		}else{
			$rs=M('goods_cats')->field("concat(catPath,catId,',') as catPath")
				->where("catId=".$parentId)->find();
			$catpath = $rs['catPath'];
		}
		
		$data = array();
		$data["catName"] = trim(I("catName"));
		
		$data["parentId"] = $parentId;
		$data["catPath"] = $catpath;

	    if($this->checkEmpty($data)){
	    	$data["priceSection"] = trim(I("priceSection"));
			$rs = M('goods_cats')->where("catId=".$catId)->save($data);
			if(false !== $rs){
				return true;
			}
		}
		return false;
	}

	/* 删除分类
	 *--------------------------*/
	 public function delGoodsCats(){
	 	$catId = (int)I('catId');
	 	$g = M('goods_cats');
	 	$check=$g->where("parentId=".$catId)->getField('catId');
	 	if($check){//有无子类
	 		return "other";
	 	}
	 	$data['catFlag']=-1;
	    $rs = $g->where("catId=".$catId)->save($data);
		if(false !== $rs){
			return true;
		}
		return false;
	 }

	 //数据处理
	 private function getJsonData($jsonData) {

		$output = json_decode($jsonData,true);
		$list = array();
		foreach ($output as $row) {
			$list[] = array(
					'id' => $row['id'],
					'parent_id' => 0,
			);
			if (isset($row['children'])) {
				$this->getChildren($list, $row['id'], $row['children']);
			}
		}

		return $list;
	}

	private function getChildren(&$list, $id, $child) {
		foreach ($child as $c) {
			$list[] = array(
					'id' => $c['id'],
					'parent_id' => $id,
			);
			if (isset($c['children']) ) {
				$this->getChildren($list, $c['id'], $c['children']);
			}
		}
		return ;
	}
    
}